package cn.com.demo.i18n.controller;

import cn.com.demo.i18n.config.MessageSourceUtils;
import cn.com.demo.i18n.service.ASyncService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@Slf4j
@RestController
@RequestMapping("/demo")
public class DemoController {

    @Autowired
    MessageSourceUtils messageSourceUtils;

    @Autowired
    private ASyncService aSyncService;

    @GetMapping("/locale")
    public String locale() {
        return messageSourceUtils.getLocale();
    }
    @GetMapping("/locale/{language}/{country}")
    public void locale(@PathVariable String language,@PathVariable String country) {
        messageSourceUtils.changeLocale(language,country);
    }
    @GetMapping("/test")
    public String test(){
        return messageSourceUtils.getMessage("user.login.name");
    }
    @GetMapping("/test1")
    public String test(String val){
        return messageSourceUtils.getMessage("user.name",new Object[]{val});
    }
    @GetMapping("/async")
    public String async() throws ExecutionException, InterruptedException {
        log.info("调用1开始");
        aSyncService.do1();
        log.info("调用1结束");
        log.info("调用2开始");
        Future<String> stringFuture = aSyncService.do2();
        log.info("调用2结束");
        aSyncService.do3();
        aSyncService.do4();
        aSyncService.do5();
        aSyncService.do6();

        return stringFuture.get();
    }
}
